#include <iostream>

using namespace std;

const int N = int(1e6 + 10);

bool empty(int q[])
{
    return q[N - 2] == q[N - 1];
}

bool full(int q[])
{
    return (q[N - 1] + 1 - q[N - 2]) % (N - 2) == 0;
}

int size(int q[])
{
    return (q[N - 1] - q[N - 2] + N - 3) % (N - 3);
}

void push(int q[], int x)
{
    if (full(q))
        return;
    q[q[N - 1]] = x;
    q[N - 1] = (q[N - 1] + 1) % (N - 2);
}

void rpush(int q[], int x)
{
    if (full(q))
        return;
    int& rtail = q[N - 2];
    int& rtop = q[N - 1];
    rtail = (rtail + N - 3) % (N - 2);
    q[rtail] = x;
}

int pop(int q[])
{
    if (empty(q))
        return ~(1 << 31);
    int& top = q[N - 2];
    int& tail = q[N - 1];
    int tmp = q[top];
    top = (top + 1) % (N - 2);
    return tmp;
}

int rpop(int q[])
{
    if (empty(q))
        return ~(1 << 31);
    int& rtop = q[N - 1];
    rtop = (rtop + N - 3) % (N - 2);
    return q[rtop];
}

int query(int q[])
{
    if (empty(q))
        return ~(1 << 31);
    int top = q[N - 2];
    return q[top];
}

int rquery(int q[])
{
    if (empty(q))
        return ~(1 << 31);
    int rtop = q[N - 1];
    rtop = (rtop + N - 3) % (N - 2);
    return q[rtop];
}

void reset(int q[])
{
    q[N - 2] = q[N - 1];
}

int a[N], b[N], q[N], p[N];

int main(void)
{
    int n, k, x;
    scanf("%d%d", &n, &k);
    if (n < 1) return 0;
    for (int i = 1; i < k; i++)
    {
        scanf("%d", &x);
        while (!empty(q) && rquery(q) < x) rpop(q);
        while (size(q) < i) push(q, x);

        while (!empty(p) && rquery(p) > x) rpop(p);
        while (size(p) < i) push(p, x);
    }

    for (int i = k; i <= n; i++)
    {
        scanf("%d", &x);
        while (!empty(q) && rquery(q) < x) rpop(q);
        while (size(q) < k) push(q, x);

        while (!empty(p) && rquery(p) > x) rpop(p);
        while (size(p) < k) push(p, x);

        push(a, pop(p));
        push(b, pop(q));
    }

    while (!empty(a)) printf("%d ", pop(a));
    printf("\n");
    while (!empty(b)) printf("%d ", pop(b));
}